package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.rest.control.ModelCropRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by LiuSha on 2020/3/24.
 * 模型库产物
 */
@RestController
@RequestMapping(value = "/model/crop")
public class ModelCropController extends BaseController {
    @Autowired
    private ModelCropRestService modelCropService;
    @Value("${traceyun.app.id}")
    private String appId;


    /**
     * 根据id加载
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/load/{id}", method = RequestMethod.GET)
    public Object load(@PathVariable("id") String id) {
        return modelCropService.load(id);
    }


    /**
     * 添加
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return modelCropService.add(map, files, UserAuthUtil.getLoginUser().getCompanyId());
    }

    /**
     * 根据id加载
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/load/category/{id}", method = RequestMethod.GET)
    public Object loadByCategoryId(@PathVariable("id") String id) {
        return modelCropService.loadByCategoryId(id);
    }

    /**
     * 绑定或者解绑作物
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/bindone", method = RequestMethod.POST)
    public Object bind(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"modelCropId", "bind"});
        params.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        params.put("appId", appId);
        return modelCropService.bindone(params);
    }

    /**
     * 加载已绑定的
     *
     * @return
     */
    @RequestMapping(value = "/loadbind", method = RequestMethod.GET)
    public Object findByCompany() {
        JSONObject param = new JSONObject();
        System.out.println(UserAuthUtil.getLoginUser().getCompanyId());
        param.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        param.put("appId", appId);
        return modelCropService.findByCompany(param);
    }

    /**
     * 加载产出物
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/load/model/{id}")
    public Object loadCropByModelCropId(@PathVariable("id") String id) {
        return modelCropService.loadCrop(id);
    }

    /**
     * 添加产出物
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/add/crop")
    public Object addCrop(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"modelId", "position", "name", "img"});
        return modelCropService.addCrop(params);
    }

    /**
     * 查询
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/search")
    public Object search(@RequestBody JSONObject params) {
        params.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        return modelCropService.search(params);
    }

    /*****************************************************************************/


    @RequestMapping(value = "/findByModelId/{modelId}", method = RequestMethod.GET)
    public Object findByAll(@PathVariable String modelId) {
        return modelCropService.findByModelId(modelId);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return modelCropService.save(map, files);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return modelCropService.update(map, files);
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return modelCropService.findById(id);
    }

    @RequestMapping(value = "/findByOneId/{id}", method = RequestMethod.GET)
    public Object findByOneId(@PathVariable String id) {
        return modelCropService.findByOneId(id);
    }

    @RequestMapping(value = "/findByTwoId/{id}", method = RequestMethod.GET)
    public Object findByTwoId(@PathVariable String id) {
        return modelCropService.findByTwoId(id);
    }


    @RequestMapping(value = "/company/save", method = RequestMethod.POST)
    public Object saveCompanyModel(@RequestBody JSONObject param) {
        param.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        param.put("appId", appId);
        return modelCropService.saveCompanyModel(param);
    }

    @RequestMapping(value = "/company/delete/{modelId}", method = RequestMethod.GET)
    public Object deleteCompanyModel(@PathVariable String modelId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("modelId", modelId);
        jsonObject.put("companyId", UserAuthUtil.getLoginUser().getCompanyId());
        return modelCropService.deleteCompanyModel(jsonObject);
    }

}
